package com.mlnx.push.websocket.server.topic;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * // bps/12345 bps/# bp/12345 bp/# ecg/12345 ecg/#
 */
public class TopicUtils {

    public static List<Topic> getTopics(String topic) {

        List<Topic> topics = new ArrayList<>();
        if (topic.contains("userId")){
            List<UserTopic> userTopics = JSON.parseArray(topic, UserTopic.class);
            topics.addAll(userTopics);
        }else if (topic.contains("deviceId")){
            List<DeviceTopic> deviceTopics = JSON.parseArray(topic, DeviceTopic.class);
            topics.addAll(deviceTopics);
        }
        return topics;
    }

    public static Topic getTopic(String topic) {
        return JSON.parseObject(topic, Topic.class);
    }

    public static boolean contain(Set<Topic> topics, Topic topic) {
        for (Topic topic1 : topics) {
            if (topic1.equals(topic)) {
                return true;
            }
        }
        return false;
    }

}
